#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：图灵学习 
@File    ：8.bs4使用.py
@IDE     ：PyCharm 
@Author  ：ChenLiGao
@Date    ：2024-11-22 16:27
'''
from bs4 import BeautifulSoup
import re

html = """
<html><head><title>The Dormouse's story1</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story2</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""

# 1.创建bs4对象，第二个参数需要手动指定一个解析器
soup = BeautifulSoup(html, 'lxml')

# 2.格式化输出页面
print(soup.prettify())

# 3. 根据标签名称获取对应标签
print("***********************************")
res_1 = soup.find_all('b')
res_2 = soup.find_all('a')
print(res_1)
print(res_2)
print("***********************************")
res_3 = soup.find_all(['b', 'a'])
print(res_3) # 将结果放在一个列表中

print("***********************************")
# 正则提取
res_4 = soup.find_all(re.compile("^b"))
print(res_4)

# 根据据属性过滤
print("***********************************")
res_5 = soup.find_all(class_ = 'sister')
print(res_5)
res_5 = soup.find_all(attrs = {'class':'sister'})
print(res_5)


print("***********************************")
res_6 = soup.find(id="link2") # 查询到第一个
print(res_6)
res_6 = soup.find_all(id="link2") # 查询列表
print(res_6)

# 根据文本获取对应的标签
res_7 = soup.find_all(string='Elsie')
print(res_7) # 获取文本值


res_8 = soup.find_all(string=['Elsie', 'Lacie', 'Tillie', 'Tillie1'])
print(res_8)


res_9 = soup.find_all(string=re.compile('Dormouse'))
print(res_9)

"""
1.创建bs4对象并手动指定解析器：lxml
2.find_all方法的特征：根据标签名称查询、属性查询、文本查询、re正则匹配查询，返回的数据类型是列表
    标签名称可以传递多个：将多个标签放到列表中直接传递给find_all方法

"""